package com.candy.candyuser.model.controller;

import com.candy.candycommon.utils.ApplicationContentUtils;
import com.candy.candycommon.utils.ResultBean;
import com.candy.candylogservice.interceptor.LogUtils;
import com.candy.candyotherservice.service.OtherService;
import com.candy.candyproductservice.dto.EsSearchDTO;
import com.candy.candyproductservice.service.QuestionService;
import com.candy.candyuser.model.service.UserManager;
import com.candy.candyuser.model.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author suYanLong
 * @date 2022年09月29日 13:36
 */
@Slf4j
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class UserController {

    private final RestTemplate restTemplate;

    @Resource
    @Lazy
    QuestionService questionService;

    private final UserManager userManager;

    private final UserService userService;

    // http://localhost:8081/candy-user/user/getUserOrder
    @GetMapping("getUserOrder")
    public ResultBean getUserOrder() {
        log.info("=============getUserOrder============");
//        ResponseEntity<ResultBean> response = restTemplate.getForEntity("http://localhost:8082/candy-order/order/getByOrderId?orderId=1", ResultBean.class);
        ResponseEntity<ResultBean> response = restTemplate.getForEntity("http://candy-order/candy-order/order/getByOrderId?orderId=1", ResultBean.class);
        return response.getBody();
    }

    // http://localhost:8081/candy-user/user/test
    //    @PostConstruct
    @GetMapping("test")
    public ResultBean test() {
        log.info("Mdc = " + LogUtils.getTraceId());
        EsSearchDTO esSearchDTO = new EsSearchDTO();
        esSearchDTO.setChannelId(202l);
        esSearchDTO.setSearchKeyWords("预约单");
        ResultBean list = questionService.getList(esSearchDTO);
        log.info("返回结果：{}", list);
        return ResultBean.ok(list);
    }

    // http://localhost:8081/candy-user/user/testAppContent
    @GetMapping("testAppContent")
    public ResultBean testAppContent() {
        UserController bean = ApplicationContentUtils.getBean(UserController.class);
        System.out.println(bean);
        return ResultBean.ok();
    }

    @GetMapping("addUser")
    public ResultBean addUser() {
        return userService.addUser();
    }

    /**
     * http://localhost:8081/candy-user/user/addUserTcc
     * seata使用TCC模式【分布式事务】【TCC】
     *
     * @return
     */
    @GetMapping("addUserTcc")
    public ResultBean addUserTcc() {
        return userManager.managerAddUser();
    }

    @GetMapping("getUserById")
    public ResultBean getUserById(@RequestParam Long id) {
        return ResultBean.ok(userService.getById(id));
    }

    OtherService otherService;

    @GetMapping("getValue")
    public String getValue() {
        try {
            String value = otherService.getValue();
            log.info(value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("getSidecar")
    public String getSidecar() {
        try {
            String value = otherService.getSidecar();
            log.info(value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
